package in.co.codedoc.idcontainer;

import in.co.codedoc.sql.IDBase;
import in.co.codedoc.sql.SessionLocal;

public class IdSetId<T extends IDBase>
	extends IDBase
{
	private IdSetId(long id)
	{
		super(id);
	}
	
	public IdSet<T> Get()
	{
		IdSet<T> entity = cachedEntityHolder.get();
		
		if(entity == null)
		{
			cachedEntityHolder.set(entity = new IdSet<T>());
			entity.SetId(this);
			entity.Reload();
		}
		
		return entity;
	}
	
	@SuppressWarnings("unchecked")
	public static<T extends IDBase> IdSetId<T> Create(long rawId)
	{
		if(rawId == Long.MIN_VALUE)
		{
			return null;
		}
		
		return (IdSetId<T>)(new IdSetId<T>(rawId).Intern());
	}
	
	private Object readResolve() 
		throws java.io.ObjectStreamException
	{
		return Create(GetId());
	}
	
	private SessionLocal<IdSet<T>> cachedEntityHolder
		= new SessionLocal<IdSet<T>>();
	
}

